<?php

class OptionsController extends BaseController {

    public function index() {

        $aData = array(
            'sPage_Title'       => trans('navigation.admin.configuration'),
            'sPage_Description' => trans('navigation.admin-description.configuration'),
        );

        return View::make("options.page", $aData);
    }

    public function getAjaxLoadPage($page) {

        $Options = Cache::get('options');
        $sLayout = 'options.table-status';

        switch ($page) {
            case 'course_option':
                $sLayout = 'options.table-course';
                $aData   = [];
                foreach ($Options['COURSE'] as $key => $value) {
                    $aData[$key] = $value;
                }
                break;

            case 'attendance':
                $sLayout = 'options.table-attendance';
                $aData   = [];
                foreach ($Options['LE'] as $key => $value) {
                    $aData['LE'][$key] = $value;
                }
                foreach ($Options['HOC'] as $key => $value) {
                    $aData['HOC'][$key] = $value;
                }
                break;

            case 'rank_option':
                $sLayout = 'options.table';
                $aData   = $Options['RANK'];
                break;

            case 'score_option':
                $aData = $Options['DOT_KIEM_TRA'];
                break;

            case 'scoretime_option':
                $aData = $Options['LAN_KIEM_TRA'];
                break;

            case 'usertype_option':
                $aData = $Options['USERTYPE'];
                break;

            case 'usercrew_option':
                $aData = $Options['USERCREW'];
                break;

            case 'relationship_option':
                $aData = $Options['RELATIONSHIP'];
                break;

            case 'team_option':
                $aData = $Options['NGANH'];
                break;

            case 'level_option':
                $aData = $Options['CAP'];
                break;

            case 'crew_option':
                $aData = $Options['DOI'];
                break;

            default:
                $aData = [];
                break;
        }

        return View::make($sLayout, [
                'page'  => $page,
                'aData' => $aData,
        ]);
    }

    public function getAjaxCreatePage($page) {

        switch ($page) {
            case 'course_option':
                $sLayout = 'options.new-course';
                break;

            case 'attendance':
                $sLayout = 'options.new-attendance';
                break;

            case 'rank_option':
                $sLayout = 'options.new';
                break;

            case 'score_option':
            case 'scoretime_option':
            case 'usertype_option':
            case 'usercrew_option':
            case 'relationship_option':
            case 'team_option':
            case 'level_option':
            case 'crew_option':
            default:
                $sLayout = 'options.new-status';
                break;
        }

        $aData = [
            'title'   => trans('options-pages.' . $page),
            'url'     => route('option-create', ['page' => $page]),
            'oOption' => new Options
        ];

        return View::make($sLayout, $aData);
    }

    function postAjaxCreatePage($page) {
        try {
            switch ($page) {
                case 'course_option':
                    $Option = [
                        'type' => 'COURSE',
                        'data' => json_encode([
                            'start' => CNamHoa::ConvertDate(Input::get('start')),
                            'end'   => CNamHoa::ConvertDate(Input::get('end')),
                        ]),
                    ];
                    break;

                case 'attendance':
                    $Option = [
                        'type'   => Input::get('type'),
                        'status' => Input::get('status'),
                        'data'   => json_encode([
                            'name'  => Input::get('name'),
                            'minus' => Input::get('minus'),
                        ]),
                    ];
                    break;

                case 'rank_option':
                    $Option = [
                        'type' => 'RANK',
                        'data' => Input::get('data'),
                    ];
                    break;

                case 'score_option':
                    $Option = [
                        'type'   => 'DOT_KIEM_TRA',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'scoretime_option':
                    $Option = [
                        'type'   => 'LAN_KIEM_TRA',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'usertype_option':
                    $Option = [
                        'type'   => 'USERTYPE',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'usercrew_option':
                    $Option = [
                        'type'   => 'USERCREW',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'relationship_option':
                    $Option = [
                        'type'   => 'RELATIONSHIP',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'team_option':
                    $Option = [
                        'type'   => 'NGANH',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'level_option':
                    $Option = [
                        'type'   => 'CAP',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                case 'crew_option':
                    $Option = [
                        'type'   => 'DOI',
                        'status' => Input::get('status'),
                        'data'   => Input::get('data'),
                    ];
                    break;

                default:
                    $Option = [];
                    break;
            }

            if (!empty($Option)) {
                $Option['key']     = Input::get('key');
                $Option['updater'] = Auth::user()->id;
                Options::create($Option);
            }

            $this->resetCacheOption();

            $aResponse = array(
                'status'  => 200,
                'message' => trans('validation.custom.success-title'),
            );
        } catch (Exception $exc) {

            $aResponse = array(
                'status'  => 0,
                'message' => trans('validation.custom.error-title'),
            );
        }

        return Response::json($aResponse);
    }

    public function getAjaxEditPage($page, $id) {
        $Option = Options::find($id);

        switch ($page) {
            case 'course_option':
                $sLayout      = 'options.new-course';
                $Option->data = json_decode($Option->data);
                break;

            case 'attendance':
                $sLayout      = 'options.new-attendance';
                $Option->data = json_decode($Option->data);
                break;

            case 'rank_option':
                $sLayout = 'options.new';
                break;

            case 'score_option':
            case 'scoretime_option':
            case 'usertype_option':
            case 'usercrew_option':
            case 'relationship_option':
            case 'team_option':
            case 'level_option':
            case 'crew_option':
            default:
                $sLayout = 'options.new-status';
                break;
        }

        return View::make($sLayout, [
                'title'   => trans('options-pages.' . $page),
                'url'     => route('option-edit', [
                    'page' => $page,
                    'id'   => $id,
                ]),
                'oOption' => $Option
        ]);
    }

    public function postAjaxEditPage($page, $id) {
        $Option = Options::find($id);

        try {
            switch ($page) {
                case 'course_option':
                    $Option->data = json_encode([
                        'start' => CNamHoa::ConvertDate(Input::get('start')),
                        'end'   => CNamHoa::ConvertDate(Input::get('end')),
                    ]);
                    break;

                case 'attendance':
                    $Option->status = Input::get('status');
                    $Option->data   = json_encode([
                        'name'  => Input::get('name'),
                        'minus' => Input::get('minus'),
                    ]);
                    break;

                case 'rank_option':
                    $Option->data = Input::get('data');
                    break;

                case 'score_option':
                case 'scoretime_option':
                case 'usertype_option':
                case 'usercrew_option':
                case 'relationship_option':
                case 'team_option':
                case 'level_option':
                case 'crew_option':
                    $Option->status = Input::get('status');
                    $Option->data   = Input::get('data');
                    break;

                default:
                    break;
            }

            $Option->updater = Auth::user()->id;
            $Option->save();
            $this->resetCacheOption();

            $aResponse = array(
                'status'  => 200,
                'message' => trans('validation.custom.success-title'),
            );
        } catch (Exception $exc) {

            $aResponse = array(
                'status'  => 0,
                'message' => trans('validation.custom.error-title'),
            );
        }
        return Response::json($aResponse);
    }

    public function postAjaxRemovePage($id) {

        $Option = Options::find($id);

        try {
            if ($Option) {
                $Option->delete();
            }
            $this->resetCacheOption();

            $aResponse = array(
                'status'  => 200,
                'message' => trans('validation.custom.success-title'),
            );
        } catch (Exception $exc) {

            $aResponse = array(
                'status'  => 0,
                'message' => trans('validation.custom.error-title'),
            );
        }
        return Response::json($aResponse);
    }

    private function resetCacheOption() {

        Cache::forget('options');
        Cache::get('options');
    }

}
